package cn.edu.xmu.talk.bean;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;

import cn.edu.xmu.cc.talk.TalkBeanRemote;
import cn.edu.xmu.cc.talk.entity.CCMessage;
import cn.edu.xmu.cc.talk.entity.Member;
import cn.edu.xmu.talk.utils.ContextManagerFactory;
import cn.edu.xmu.talk.utils.ManagedBeanFinder;

public class MessageBean {
	
	public static void send(String content,String frindId,String id){
		int fid = Integer.parseInt(frindId);
		int myId = Integer.parseInt(id);
		CCMessage message = new CCMessage();
		message.setContent(content);
		Member receiver = new Member();
		receiver.setUserId(fid);
		Member sender = new Member();
		sender.setUserId(myId);
		message.setReceiver(receiver);
		message.setSender(sender);
		if(fid == -1){
			sendAll(message);
		}else{
			send(message);
		}
	}
	public static void send(CCMessage cm){
		cm.setPersonal();
		ManagedBeanFinder.sendMessage(cm);
	}
	
	public static void sendAll(CCMessage cm){
		cm.setGroup();
		ManagedBeanFinder.sendMessage(cm);
	}
	
	public void register(ActionEvent event){
		Member member = (Member) ManagedBeanFinder.findBean("member");
		TalkBeanRemote bean = ContextManagerFactory.getSessionBean();
		Member result = bean.register(member);
		FacesContext facesContext = FacesContext.getCurrentInstance();
		ExternalContext externalContext = facesContext.getExternalContext();
		Map<String,Object> sessionMap=externalContext.getSessionMap();
		sessionMap.put("userId",member.getUserId());
		sessionMap.put("userName",member.getUserName());
	}
	
	public String login(){
		Member member = (Member)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("member");
		TalkBeanRemote bean = ContextManagerFactory.getSessionBean();
		Member result = bean.login(member);
		if(result != null){
			FacesContext facesContext = FacesContext.getCurrentInstance();
			ExternalContext externalContext = facesContext.getExternalContext();
			Map<String,Object> sessionMap=externalContext.getSessionMap();
			sessionMap.put("userId",result.getUserId());
			sessionMap.put("userName",result.getUserName());
			member.setUserId(result.getUserId());
			return "success";
			
		}
		return "failed";
	}
	
	private int friendId;
	
	
	public int getFriendId() {
		return friendId;
	}

	public void setFriendId(int friendId) {
		this.friendId = friendId;
	}

	public List<SelectItem> getAllUsers(){
		int userId = Integer.parseInt(ManagedBeanFinder.findBean("member.userId").toString());
		List<Member> list = ContextManagerFactory.getSessionBean().getAllUsers(userId);
		List<SelectItem> itemList = new ArrayList<SelectItem>();
		for(int i = 0;i < list.size();++i){
			SelectItem item = new SelectItem();
			item.setValue(list.get(i).getUserId());
			item.setLabel(list.get(i).getUserName());
			itemList.add(item);
		}
		return itemList;
	}
	
	public void chooseFriend(ValueChangeEvent event){
		System.out.println("event happend!");
	}
	
	
}
